<template>
  <div class="main-sidebar-height" style="padding-bottom: 24px; background-color: #fff">
    <PageHeader @back="goback" subTitle="返回">{{ tempInfo.name }} 批量导入</PageHeader>
    <div v-if="errorLst === null || errorLst.length === 0">
      <a-spin :spinning="uploading">
        <div class="ant-upload-select-text-box" style="padding-top: 24px">
          <a-upload name="file" :multiple="false" accept=".xls, .xlsx, .xlsm" :show-upload-list="false" :before-upload="beforeUpload">
            <img :src="uploadImg" style="width: 139px; height: 106px" />
            <div class="ant-modal-body">
              <div style="font-size: 12px; color: #666">点击添加文件或将文件拖拽到此处</div>
              <div style="font-size: 12px; color: #999">(文件大小不超过5M)</div>
            </div>
          </a-upload>
        </div>
      </a-spin>
      <div class="tipBox">
        <a-row style="color: #252525; font-size: 16px; font-weight: 500"> 温馨提示 </a-row>
        <a-row>
          <a-col :span="12">
            <div style="margin-top: 16px">
              <span>1. 首先下载标准模板，填写信息后再上传 </span>
              <a-button type="link" @click="download" class="primary_color" style="font-size: 12px; height: 18px">点击下载模板</a-button>
            </div>
            <!-- <div>1.支持WPS、excel2003以上版本文件</div> -->
            <div class="rowMargin">2. 接单时间、接单客服、所属用工单位、姓名、身份证号为必填项，顺序可调整，但不可删除</div>
            <div class="rowMargin">3. 若Excel文件设有权限密码，需取消Excel文件密码，否则系统无法识别</div>
          </a-col>
        </a-row>
      </div>
    </div>
    <div v-else style="display: flex; padding-top: 15px; padding-left: 24px; padding-right: 24px; flex-direction: column; width: 100%">
      <div>
        <div
          style="
            padding-left: 10px;
            display: flex;
            flex-flow: row;
            align-items: center;
            height: 35px;
            width: 100%;
            background-color: #fffbe6;
            border-style: solid;
            border-color: #ffe58f;
            border-width: 1px;
            border-radius: 5px;
          "
        >
          <img style="width: 16px; height: 16px; margin-right: 5px" src="@/assets/icons/重复.png" />
          <span sytle="color:#55565D;font-size:14px;">
            共{{ allCount }}条数据，成功导入{{ successCount }}条数据， <label class="redc">{{ errorCount }}</label
            >条数据导入失败
          </span>
        </div>
      </div>
      <div class="header-title-box">
        <div class="headerTitle">
          <div class="block" />
          <div style="font-size: 15px; font-weight: 700; font-color: #252525">错误信息</div>
        </div>
        <div class="btn-box">
          <a-button class="fr" type="primary" @click="batchGet" :disabled="uploading">{{
            uploading ? "数据导出中..." : "导出错误数据"
          }}</a-button>
        </div>
      </div>
      <a-table :columns="columns" :data-source="errorLst" :scroll="{ x: 800 }" row-key="身份证号" />
    </div>
    <a-modal v-model="dialogFailVisible" title="批量导入" width="500px" @ok="handleCancel">
      <div style="padding-left: 10px; display: flex; flex-flow: row; align-items: center; height: 35px; width: 100%">
        <img style="width: 25px; height: 25px; margin-right: 10px" src="@/assets/icons/duihao.png" />
        <div style="color: #3c3d43; font-size: 15px; font-weight: 700">共 {{ allCount }} 条数据，全部导入成功！</div>
      </div>
    </a-modal>
  </div>
</template>
<script>
import UploadMixin from "xqjr-plugin-common/UploadOrExport/uploadMixin";
import axios from "axios";
const mixin = new UploadMixin();
export default {
  mixins: [mixin],
  data() {
    return {
      columns: [],
      excelName: "批量导入模板.xlsx",
      tempInfo: {},
      uploading: false,
      allCount: 0,
      errorCount: 0,
      successCount: 0,
      uploadImg: require("@/assets/img/uploadImg.png"),
      action: "",
      socFile: undefined,
      dialogFailVisible: false,
      errorLst: [],
      errorDataFull: null
    };
  },
  computed: {
    url() {
      return `/hr/api/v1/treatmentDisposeTemplates/generateExcel/${this.tempInfo.id}`;
    }
  },
  created() {
    this.tempInfo = JSON.parse(localStorage.getItem("importDaiyuchuliItem"));
    this.action = `/hr/api/v1/treatmentDisposeEvents/importEvent/${this.tempInfo.id}`;

    this.errorLst = [];
  },
  methods: {
    handleCancel() {
      this.dialogFailVisible = false;
      this.goback();
    },
    handleSuccess(res) {
      this.uploading = false;
      if (res && res.code === 200) {
        this.allCount = res.data.allCount;
        this.errorCount = res.data.errorCount;
        this.successCount = res.data.successCount;
        this.errorLst = res.data.errorList;
        this.errorDataFull = res.data;
        this.columns = res.data.headList.map((title) => ({
          title,
          dataIndex: title,
          key: title,
          width: 150
        }));
        if (this.errorLst == null || this.errorLst.length === 0) {
          this.dialogFailVisible = true;
        }
      } else {
        this.$message.error(res.msg);
      }
    },
    handleUploadError() {
      this.$message.warning("没有文件");
    },
    batchGet() {
      this.uploading = true;
      const url = "/hr/api/v1/treatmentDisposeEvents/exportErrorList";
      axios({
        url,
        data: this.errorDataFull,
        method: "post",
        responseType: "blob",
        headers: {
          token: this.$store.state.auth.token,
          "Content-Type": "application/json"
        }
      })
        .then((res) => {
          this.uploading = false;
          const link = document.createElement("a");
          const blob = new Blob([res.data], {
            type: "application/vnd.ms-excel"
          });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.setAttribute("download", "事件导入错误.xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.main-sidebar-height {
  min-height: calc(100vh);
}
.header-title-box {
  margin-top: 10px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .headerTitle {
    display: flex;
    justify-content: flex-start;
    .block {
      width: 3px;
      height: 20px;
      margin-top: 2px;
      background-color: var(--primary);
      margin-right: 12px;
    }
  }
}
.tipBox {
  line-height: 18px;
  background: #faf9f9;
  border-radius: 4px;
  font-size: 12px;
  color: #595959;
  margin: 0 24px;
  padding: 12px;
  height: 200px;
}
.ant-upload-select-text-box {
  border: 1px dashed #b8b8b8;
  background: #fff;
  margin: 24px;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
  padding: 12px;
}
.ant-upload-select-text-box ::v-deep .ant-upload-list {
  display: none;
}

.ant-modal-body {
  line-height: 24px;

  em {
    color: var(--primary);
    font-style: normal;
  }
}
.redc {
  color: red;
}
</style>
